package org.neuedu.ssmp.crm.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.neuedu.ssmp.crm.entity.Chances;
import org.neuedu.ssmp.crm.entity.Opition;
import org.neuedu.ssmp.crm.entity.SysRole;
import org.neuedu.ssmp.crm.entity.SysUser;
import org.neuedu.ssmp.crm.entity.vo.ResponseEntity;
import org.neuedu.ssmp.crm.service.IServeService;
import org.neuedu.ssmp.crm.service.ISysRoleService;
import org.neuedu.ssmp.crm.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MoonHolder
 * @since 2019-12-09
 */
@RestController
@Slf4j
@RequestMapping("/sys-user")
public class SysUserController {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService sysRoleService;


    @GetMapping("list")
    public List<SysUser> list(){
        return userService.lists();
    }
    @PostMapping("lists")
    public SysUser sysUserList(@RequestParam String username, @RequestParam String pwd, HttpSession session){
        SysUser sysUser = new SysUser();
        sysUser.setUsername(username);
        sysUser.setPassword(pwd);
        session.setAttribute("sysUser",sysUser);
        return userService.login(username,pwd);
    }
    @PostMapping("getrole")
    public SysRole sysUserList(@RequestParam Integer id,@RequestParam String name){
        return userService.select(id,name);
    }
    @GetMapping("show")
    public List<SysUser> show(){
        return userService.show();
    }

    @GetMapping("roles")
    public Opition[] roles(){
        List<SysRole> list = sysRoleService.list();
        Opition[] roleList=new Opition[list.size()];
        for (int i = 0; i < list.size(); i++) {
            Opition build = Opition.builder().text(list.get(i).getName()).value(list.get(i).getName()).roleId(list.get(i).getId()).build();
            roleList[i]=build;
        }
        return roleList;
    }

    @GetMapping("save")
    public ResponseEntity save(SysUser sysUser){
        System.out.println("==============");
        List<SysUser> exist = userService.exist(sysUser.getUsername());
        if (exist.size()!=0){
            return ResponseEntity.error("已经存在此用户，注册失败");
        }
        boolean save = userService.save(sysUser);
        if (!save) {
            return ResponseEntity.error("新增用户保存失败");
        }
        return ResponseEntity.ok("新增用户保存成功");
    }

    @GetMapping("del")
    public ResponseEntity del(int id){
        boolean i = userService.deleteById(id);
        if (i){
            return ResponseEntity.ok("删除成功");
        }
        return ResponseEntity.error("删除失败");
    }

    @GetMapping("updateByid")
    public ResponseEntity updateByid(SysUser sysUser){
        log.info(sysUser.toString());
        boolean i = userService.updateById(sysUser);
        if (i){
            return ResponseEntity.ok("更新成功");
        }
        return ResponseEntity.error("更新失败");
    }

}

